package com.zhiyou.service.imp;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alipay.api.AlipayApiException;
import com.zhiyou.mapper.OrderMapper;
import com.zhiyou.mapper.TeacherMapper;
import com.zhiyou.model.Order;
import com.zhiyou.model.Teacher;
import com.zhiyou.service.AliPayService;
import com.zhiyou.utils.AlipayConfig;

@Service
public class AliPayServiceImp implements AliPayService {

	@Autowired
	OrderMapper ordermapper;
	
	@Autowired
	TeacherMapper teachermapper;
	
	public String pay(Integer teacher_id, String order_money) throws IOException, AlipayApiException {
		// 商户订单号，商户网站订单系统中唯一订单号，必填
				String out_trade_no = UUID.randomUUID().toString();// 牛逼，每秒10亿次，100年才能重复一次
				// 订单名称，必填
				String subject = "飞象充值";
				String orderbody = "一卡通充值";
				String result = AlipayConfig.pay(out_trade_no,order_money,subject,orderbody);
				System.out.println();
				// 把订单信息添加到数据中
				Order order = new Order();
				order.setOrder_id(null);
				order.setOut_trade_no(out_trade_no);;
				order.setOrdersubject(subject);
				order.setOrderbody(orderbody);
				order.setDeal_status(false);
				order.setOrder_money(order_money);
				order.setDeal_time(new Date());
				order.setTeacher_id(teacher_id);
				order.setStudent_id(null);
				order.setTrade_no(null);
				ordermapper.add(order);
				return result;
	}

	public Boolean updateOrder(Map<String, String[]> requestParams,Integer teacher_id) {
		Map<String, String> params = new HashMap<String, String>();
		for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
			String name = (String) iter.next();
			String[] values = (String[]) requestParams.get(name);
			String valueStr = "";
			for (int i = 0; i < values.length; i++) {
				valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
			}
			// 乱码解决，这段代码在出现乱码时使用
			try {
				valueStr = new String(valueStr.getBytes("utf-8"), "utf-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			params.put(name, valueStr);
		}
		// 先根据订单号查询到订单信息
		Order order = ordermapper.selectByid(params.get("out_trade_no"));
		// 调用支付
		Boolean isPay = AlipayConfig.updateOrder(params);
		// 判断是否支付成功
		if (isPay) {
			// 修改订单信息
			order.setDeal_status(true);
			System.out.println(order);
			ordermapper.update(order);
			// 把用户余额增加
			Teacher teacher = teachermapper.selectByid(teacher_id);
			Double money = teacher.getTeacher_balance();
			Double totalAmount = Double.valueOf(order.getOrder_money());
			money += totalAmount;
			teacher.setTeacher_balance(money);
			teachermapper.updateBalance(teacher);
			return true;
		}
		return false;
	}

}
